package com.oozol.lieluyun.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.oozol.lieluyun.config.AppConfig;
import com.oozol.lieluyun.entity.ImageTagDel;
import com.oozol.lieluyun.entity.ImageTags;
import com.oozol.lieluyun.entity.ImageTagsEntity;
import com.oozol.lieluyun.entity.RegistryImages;
import com.oozol.lieluyun.entity.Repositories;
import com.oozol.lieluyun.service.IRegistryService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class RegistryServiceImpl implements IRegistryService {
   @Autowired
   private AppConfig appConfig;
   @Autowired
   private RestTemplate restTemplate;

   public List<RegistryImages> getImages() throws Exception {
      List<RegistryImages> list = new ArrayList();
      String urlFilters = "/v2/_catalog";
      Map<String, Integer> map = new HashMap();
      ResponseEntity<Repositories> forEntity = this.restTemplate.getForEntity(this.appConfig.getRegistryServerURI() + urlFilters, Repositories.class, map);
      Repositories body = (Repositories)forEntity.getBody();
      body.getRepositories().forEach((item) -> {
         RegistryImages registryImages = new RegistryImages();
         registryImages.setRepository(item);
         list.add(registryImages);
      });
      return list;
   }

   public List<ImageTags> getImageTags(String repositoryName) throws Exception {
      List<ImageTags> list = new ArrayList();
      String urlFilters = "/v2/" + repositoryName + "/tags/list";
      Map<String, Integer> map = new HashMap();
      ResponseEntity<ImageTagsEntity> forEntity = this.restTemplate.getForEntity(this.appConfig.getRegistryServerURI() + urlFilters, ImageTagsEntity.class, map);
      ImageTagsEntity forEntityBody = (ImageTagsEntity)forEntity.getBody();
      if (forEntityBody.getTags() == null) {
         return list;
      }
      forEntityBody.getTags().forEach((item) -> {
         ImageTags imageTags = new ImageTags();
         imageTags.setName(forEntityBody.getName());
         imageTags.setTag(item);
         list.add(imageTags);
      });
      list.stream().sorted(Comparator.comparing(ImageTags::getTag));
      Collections.reverse(list);
      return list;
   }

   public Integer delImageTag(ImageTagDel imageTagDel) throws Exception {
      String urlFilters = "/v2/" + imageTagDel.getRepositoryName() + "/manifests/" + imageTagDel.getTag();
      String url = this.appConfig.getRegistryServerURI() + urlFilters;
      HttpHeaders requestHeaders = new HttpHeaders();
      requestHeaders.add("Accept", "application/vnd.docker.distribution.manifest.v2+json");
      HttpEntity<String> requestEntity = new HttpEntity((Object)null, requestHeaders);
      ResponseEntity<JSONObject> forEntity = this.restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class, new Object[0]);
      HttpHeaders headers = forEntity.getHeaders();
      List<String> strings = headers.get("Docker-Content-Digest");
      String sha256 = (String)strings.get(0);
      String delUrlFilters = "/v2/" + imageTagDel.getRepositoryName() + "/manifests/" + sha256;
      String delUrl = this.appConfig.getRegistryServerURI() + delUrlFilters;
      this.restTemplate.delete(delUrl, new Object[0]);
      return 1;
   }
}
